package data;
/**
 * three dimensional float vector
 * @author Fennes
 *
 */
public class Vector3 {
	
	public float x, y, z;
	/**
	 * x,y and z are set to 0
	 */
	public Vector3 () {
		x = 0;
		y = 0;
		z = 0;
	}
	/**
	 * vector constructor. sets x, y and z components to the given parameters
	 * @param x x
	 * @param y y
	 * @param z z
	 */
	public Vector3 (float x, float y, float z) {
		this.x = x;
		this.y = y;
		this.z = z;
	}
	/**
	 * sets x, y and z components to the given parameters
	 * @param x x
	 * @param y y
	 * @param z z
	 */
	public void set(float x, float y, float z) {
		this.x = x;
		this.y = y;
		this.z = z;
	}
	/**
	 * calculates the distance between the two points specified by this vector and other
	 * @param other other vector interpreted as point
	 * @return distance
	 */
	public float distance(Vector3 other){
		return (float)Math.sqrt(Math.pow(this.x-other.x,2) + Math.pow(this.y-other.y,2) + Math.pow(this.z-other.z,2));
	}
	/**
	 * normalizes this vector
	 * @return this
	 */
	public Vector3 normalize(){
		float length = 1/this.length();
		this.x *= length;
		this.y *= length;
		this.z *= length;
		return this;
	}
	/**
	 * returns the length of the vector
	 * @return length
	 */
	public float length(){
		return (float)Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2) + Math.pow(this.z, 2) );
	}
	public Vector3 sub(Vector3 other){
		this.x -= other.x;
		this.y -= other.y;
		this.z -= other.z;
		return this;
	}
	/**
	 * subtracts this from other and stores result in this
	 * @param other vector from which this is subtracted
	 * @return this
	 */
	public Vector3 subThisFrom(Vector3 other){
		this.x = (other.x - this.x);
		this.y = (other.y - this.y);
		this.z = (other.z - this.z);
		return this;
	}
	/**
	 * adds this to other vector and stores result in this
	 * @param other other vector that is added
	 * @return this
	 */
	public Vector3 add(Vector3 other){
		this.x += other.x;
		this.y += other.y;
		this.z += other.z;
		return this;
	}
	/**
	 * multiplies this with scalar and stores result in this
	 * @param scalar 
	 * @return the vector = result
	 */
	public Vector3 mult(float scalar){
		this.x *= scalar;
		this.y *= scalar;
		this.z *= scalar;
		return this;
	}
	/**
	 * copies the value of this and returns a clone
	 */
	public Vector3 clone(){
		Vector3 vec = new Vector3();
		vec.set(this.x, this.y, this.z);
		return vec;
	}
}
